package com.ck.chuangke.controller;

import com.ck.chuangke.service.CkCompanyService;
import com.ck.chuangke.service.CkProgramService;
import com.ck.constant.PageConstant;
import com.ck.export.util.SimpleExportUtil;
import com.ck.model.chuangke.vo.CkCompanySelectListVO;
import com.ck.model.chuangke.vo.CkProgramExportVO;
import com.ck.model.chuangke.vo.CkProgramListVO;
import com.ck.util.DateUtils;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;

/**
 * 创客-节目
 *
 * @author linqiang
 * @date 2018-06-20 18:19
 */
@Controller
@RequestMapping(value = "/ckProgram")
public class CkProgramController {
    private Logger logger = LoggerFactory.getLogger(CkProgramController.class);
    @Autowired
    private CkProgramService ckProgramService;
    @Autowired
    private CkCompanyService ckCompanyService;

    /**
     * 分页查询
     *
     * @param programStatus 节目状态
     * @param companyCode   商家编号
     * @param programName   节目名称
     * @param startTime     更新开始时间（格式：yyyy/mm/dd）
     * @param endTime       更新结束时间（格式：yyyy/mm/dd）
     * @param model         模型
     * @param pageNum       当前页
     * @param pageSize      每页条数
     * @return
     */
    @RequestMapping(value = "/listByPage")
    public String listByPage(Integer programStatus, String companyCode, String programName, String startTime, String endTime,
                             Model model, Integer pageNum, Integer pageSize) {
        try {
            pageNum = (pageNum == null ? PageConstant.DEFAULT_PAGE_NUM : pageNum);
            pageSize = (pageSize == null ? PageConstant.DEFAULT_PAGE_SIZE : pageSize);
            Long startTimeL = null;
            Long endTimeL = null;
            if (StringUtils.isNotBlank(startTime)) {
                startTimeL = DateUtils.stringTimeToLongTimeStamp(startTime, DateUtils.PATTEN_YMD_HMS1);
            }
            if (StringUtils.isNotBlank(endTime)) {
                endTimeL = DateUtils.stringTimeToLongTimeStamp(endTime, DateUtils.PATTEN_YMD_HMS1);
            }
            Page<CkProgramListVO> page = ckProgramService.listByPage(programStatus, companyCode, programName, startTimeL, endTimeL, pageNum, pageSize);
            List<CkCompanySelectListVO> ckCompanySelectListVOList = ckCompanyService.listSelect();
            model.addAttribute("pageNum", pageNum);
            model.addAttribute("pageSize", pageSize);
            model.addAttribute("result", page.getResult());
            model.addAttribute("total", page.getTotal());
            model.addAttribute("programStatus", programStatus);
            model.addAttribute("companyCode", companyCode);
            model.addAttribute("programName", programName);
            model.addAttribute("startTime", startTime);
            model.addAttribute("endTime", endTime);
            model.addAttribute("companySelectList", ckCompanySelectListVOList);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("CkProgramController listByPage fail:" + e.getMessage());
        }
        return "ck/ck_program_list";
    }

    /**
     * 创客导出节目列表
     *
     * @param programStatus 节目状态
     * @param companyCode   商家编码
     * @param programName   节目名称
     * @param startTime     更新开始时间
     * @param endTime       更新结束时间
     * @param response      响应
     */
    @RequestMapping(value = "/exportCkProgram")
    public void exportProgramCk(Integer programStatus, String companyCode, String programName, String startTime, String endTime, HttpServletResponse response) {
        try (OutputStream out = response.getOutputStream()) {
            Long startTimeL = null;
            Long endTimeL = null;
            if (StringUtils.isNotBlank(startTime)) {
                startTimeL = DateUtils.stringTimeToLongTimeStamp(startTime, DateUtils.PATTEN_YMD_HMS1);
            }
            if (StringUtils.isNotBlank(endTime)) {
                endTimeL = DateUtils.stringTimeToLongTimeStamp(endTime, DateUtils.PATTEN_YMD_HMS1);
            }
            List<CkProgramExportVO> ckProgramExportVOList = ckProgramService.listExportProgramCk(programStatus, companyCode, programName, startTimeL, endTimeL);
            //设置输出类型
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            //设置文件名
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(("创客节目列表.xls").getBytes(), "ISO-8859-1"));
            SimpleExportUtil.exportToExcel(ckProgramExportVOList, "创客节目列表").write(out);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("CkProgramController exportCkProgram 导出创客节目列表：" + e.getMessage());
        }
    }
}
